package com.zc.client.http;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONString;
import cn.hutool.json.JSONUtil;
import com.zc.client.http.enums.ServiceType;
import com.zc.client.http.enums.SourceType;
import com.zc.client.http.form.LoginFrom;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * 送礼用的Client
 *
 * @author wzy
 * @Date 2025/8/28 14:00
 */

public class SendGiftClient implements Client<HttpRequest> {

    private final SourceType sourceType;
    private final ServiceType serviceType;
    private String port;
    private HashMap<String,String> paramsMap = new HashMap<>();
    private String params = "t=B&";
    private HttpRequest httpRequest;


    public SendGiftClient(SourceType sourceType, ServiceType serviceType, String port) {
        this.sourceType = sourceType;
        this.serviceType = serviceType;
        this.port = port;
    }

    @Override
    public HttpRequest creatPost() {
        HttpRequest post = HttpUtil.createPost(sourceType.getUrl() + (this.port != null ?":" + serviceType.getPort() : "") + port);
        return this.httpRequest = post;
    }

    @Override
    public String fillSession(String mobile) {
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        // 这单纯只是为了登录用的
        String loginPort = "/user/acct/login/mobile";
        String queryParam = "t=B&s=a536964b9d984ae5821832db7b54a971&t=I&v=1.0.1.1&d=XX iPhone&m=Mi 6&n=wifi&r=cmcc&o=ios10.1&i=fe123ab212cd1321bcam1&h=13798121516&c=WDGG&e=123&L=zh&";
        objectObjectHashMap.put("X-INF", queryParam);
        String loginParam = new LoginFrom.Builder().code("1234").mobile(mobile).build().generateJSONStr();
        try(HttpResponse execute = HttpUtil.createPost(sourceType.getUrl() + ":" + serviceType.getPort() + loginPort).addHeaders(objectObjectHashMap).body(loginParam).execute()){
            HashMap<String, Object> loginVO = JSONUtil.toBean(execute.body(), HashMap.class);
            String o = JSONUtil.toBean(loginVO.get("data").toString(), HashMap.class).get("sessionId").toString();
            this.paramsMap.put("s", o);
            return o;
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    @Override
    public HttpResponse postExecute(ParaForm paraForm) {
        String paramStr = JSONUtil.toJsonStr(paraForm);
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        generateParams();
        objectObjectHashMap.put("X-INF", this.params);
        try (HttpResponse execute = this.httpRequest.addHeaders(objectObjectHashMap).body(paramStr).execute()) {
            byte[] bytes = execute.bodyBytes();
            Charset charset = Charset.forName(String.valueOf(StandardCharsets.UTF_8));
            String s = new String(bytes, charset);
            System.out.println(s);
            return execute;
        }catch (Exception e){
            throw new  RuntimeException(e);
        }
    }

    @Override
    public void setSessionId(String sessionId) {
        this.paramsMap.put("s", sessionId);
    }

    @Override
    public void setPort(String port) {
        this.port = port;
    }

    public void generateParams() {
        this.paramsMap.put("t", "I");
        this.paramsMap.put("v", "1.0.1.1");
        this.paramsMap.put("d", "XX iPhone");
        this.paramsMap.put("m", "Mi 6");
        this.paramsMap.put("n", "wifi");
        this.paramsMap.put("r", "cmcc");
        this.paramsMap.put("o", "ios10.1");
        this.paramsMap.put("i", "fe123ab212cd1321bcam1");
        this.paramsMap.put("h", "13798121516");
        this.paramsMap.put("c", "WDGG");
        this.paramsMap.put("e", "123");
        this.paramsMap.put("L", "zh");
        this.paramsMap.forEach((k, v)->{
            this.params = this.params +  k + "=" + v + "&";
        });
    }
}
